package org.hy.framework.spring.hibernate3.util.db;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.hy.commons.io.core.IoUtil;

public class DbUtil {
	public static Connection getConnection() throws SQLException {
		Connection con = null;
		File propertyFilePath = IoUtil.findFile(IoUtil.getProjectPath(), "jdbc.properties");

		String  jdbc_url = IoUtil.readProperty(propertyFilePath, "jdbc.url");
		String	jdbc_username = IoUtil.readProperty(propertyFilePath, "jdbc.username");
		String	jdbc_password =  IoUtil.readProperty(propertyFilePath, "jdbc.password");
		con = DriverManager.getConnection(jdbc_url,jdbc_username, jdbc_password);
		//con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", 
		//		"root", "123456");
		return con;
	}
	
	public static void close(Connection con) {
		try {
			if(con!=null) con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void close(PreparedStatement ps) {
		try {
			if(ps!=null) ps.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void close(ResultSet rs) {
		try {
			if(rs!=null) rs.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
